/* eslint-disable */

export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}
/**
 * 身份证验证
 * @param {string} str
 * @returns {Boolean}
 */
 export function validIDCard (str) {
  var idCard = str
  var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1] // 加权因子
  var ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2] // 身份证验证位值.10代表X
  var isTrueValidateCodeBy18IdCard = function (idCard) {
    var sum = 0 // 声明加权求和变量
    var a_idCard = idCard.split('')
    if (a_idCard[17].toLowerCase() === 'x') {
      a_idCard[17] = 10 // 将最后位为x的验证码替换为10方便后续操作
    }
    for (var i = 0; i < 17; i++) {
      sum += Wi[i] * a_idCard[i] // 加权求和
    }
    if (Number(a_idCard[17]) === ValideCode[sum % 11]) {
      return true
    } else {
      return false
    }
  }
  var isValidityBrithBy18IdCard = function (idCard18) {
    var year = idCard18.substring(6, 10)
    var month = idCard18.substring(10, 12)
    var day = idCard18.substring(12, 14)
    var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day))
    // 这里用getFullYear()获取年份，避免千年虫问题
    if (temp_date.getFullYear() !== parseFloat(year) ||
      temp_date.getMonth() !== parseFloat(month) - 1 ||
      temp_date.getDate() !== parseFloat(day)) {
      return false
    } else {
      temp_date = year + '-' + month + '-' + day
      return true
    }
  }
  var isValidityBrithBy15IdCard = function (idCard15) {
    var year = idCard15.substring(6, 8)
    var month = idCard15.substring(8, 10)
    var day = idCard15.substring(10, 12)
    var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day))
    // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
    if (temp_date.getYear() !== parseFloat(year) || temp_date.getMonth() !== parseFloat(month) - 1 || temp_date.getDate() !==
      parseFloat(day)) {
      return false
    } else {
      return true
    }
  }
  if (idCard) {
    if (/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/.test(idCard)) {
      if (idCard.length === 15 && isValidityBrithBy15IdCard(idCard)) {
        return true
      } else if (idCard.length === 18 && isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(idCard)) {
        return true
      } else {
        return false
      }
    } else {
      return false
    }
  }
}

/**
 * 统一社会信用代码
 * @param {string} str
 * @returns {Boolean}
 */
 export function validCreditCode(value) {
  var cardno = value.trim()
  var patrn = /^[0-9A-Z]+$/
  // 18位校验及大写校验
  if ((cardno.length !== 18) || (patrn.test(cardno) === false)) {
    return false
  } else {
    var Ancode // 统一社会信用代码的每一个值
    var Ancodevalue // 统一社会信用代码每一个值的权重
    var total = 0
    var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28] // 加权因子
    var str = '0123456789ABCDEFGHJKLMNPQRTUWXY'
    // 不用I、O、S、V、Z
    for (let i = 0; i < cardno.length - 1; i++) {
      Ancode = cardno.substring(i, i + 1)
      Ancodevalue = str.indexOf(Ancode)
      total = total + Ancodevalue * weightedfactors[i]
      // 权重与加权因子相乘之和
    }
    var logiccheckcode = 31 - total % 31
    if (logiccheckcode === 31) {
      logiccheckcode = 0
    }
    var Str = '0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y'
    var Array_Str = Str.split(',')
    logiccheckcode = Array_Str[logiccheckcode]
    var checkcode = cardno.substring(17, 18)
    if (logiccheckcode !== checkcode) {
      // console.info("不是有效的统一社会信用编码！");
      return false
    } else {
      // console.info("yes")
    }
    return true
  }
}

/**
 * 特殊身份证验证
 * @param {string} str
 * @returns {Boolean}
 */
export function validIDCardother (str) {
  const pattern = /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}

/**
 * 手机号验证
 * @param {string} mvalue
 * @returns {Boolean}
 */

export function validTelphone (mvalue) {
  // 移动号段
  // var pattern1 = /^0{0,1}(13[4-9]|14[37]|15[012789]|18[23478]|17[028]|198)[0-9]{8}$/
  // 联通号段
  // var pattern2 = /^0{0,1}(13[0-2]|14[56]|15[56]|166|17[156]|18[56])[0-9]{8}$/
  // 电信号段
  // var pattern3 = /^0{0,1}(133|141|153|17[0347]|18[019]|19[19])[0-9]{8}$/
  // 携号转网不区分号段
  const pattern = /^0{0,1}(13[0-9]|14[13-7]|15[0-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])[0-9]{8}$/
  if (mvalue !== '' && !pattern.test(mvalue)) {
    return false
  } else {
    return true
  }
}

/**
 * 密码验证
 * @param {string} str
 * @returns {Boolean}
 */

export function validPassword (str) {
  const pattern =
    /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$^&*`~()-+=]+$)(?![0-9\W_!@#$^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$^&*`~()-+=]{8,20}$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}

/**
 * 固定电话验证
 * @param {string} str
 * @returns {Boolean}
 */
export function validFixedTelphone (str) {
  const pattern = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}


/**
 * 是否为数字
 * @param {string} str
 * @returns {Boolean}
 */
export function validNumber(str) {
  const pattern = /^(([0-9])|([1-9]([0-9]+)))(.[0-9]+)?$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}

/**
 * 是否为浮点数
 * @param {string} str
 * @returns {Boolean}
 */
 export function validFloatNumber(str) {
  const pattern = /(?:^[1-9]([0-9]+)?(?:\.[0-9]{1,2})?$)|(?:^(?:0)$)|(?:^[0-9]\.[0-9](?:[0-9])?$)/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}

/**
 * 银行卡验证
 * @param {string} str
 * @returns {Boolean}
 */
 export function validateBankCard(str) {
  const pattern = /^([1-9]{1})(\d{14}|\d{18})$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}


/**
 * 银行卡验证
 * @param {string} str
 * @returns {Boolean}
 */
 export function validBankCard(bankno) {
  var lastNum = bankno.substr(bankno.length - 1, 1); //取出最后一位（与luhn进行比较）
  var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
  var newArr = new Array();
  for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存进数组
    newArr.push(first15Num.substr(i, 1));
  }
  var arrJiShu = new Array(); //奇数位*2的积 <9
  var arrJiShu2 = new Array(); //奇数位*2的积 >9
  var arrOuShu = new Array(); //偶数位数组
  for (var j = 0; j < newArr.length; j++) {
    if ((j + 1) % 2 == 1) { //奇数位
        if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
        else arrJiShu2.push(parseInt(newArr[j]) * 2);
    } else //偶数位
    arrOuShu.push(newArr[j]);
  }
  var jishu_child1 = new Array(); //奇数位*2 >9 的分割之后的数组个位数
  var jishu_child2 = new Array(); //奇数位*2 >9 的分割之后的数组十位数
  for (var h = 0; h < arrJiShu2.length; h++) {
    jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
    jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
  }
  var sumJiShu = 0; //奇数位*2 < 9 的数组之和
  var sumOuShu = 0; //偶数位数组之和
  var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
  var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
  var sumTotal = 0;
  for (var m = 0; m < arrJiShu.length; m++) {
    sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
  }
  for (var n = 0; n < arrOuShu.length; n++) {
    sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
  }
  for (var p = 0; p < jishu_child1.length; p++) {
    sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
    sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
  }
  //计算总和
  sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
  //计算luhn值
  var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
  var luhn = 10 - k;
  if (lastNum == luhn) {
    return true;
  } else {
    return false;
  }
}

/**
 * 用户名
 * @param {string} str
 * @returns {Boolean}
*/
export function validAccountNumber (str) {
  const pattern = /^[a-zA-Z][a-zA-Z0-9_]{3,29}$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}


/**
 * 中文
 * @param {string} str
 * @returns {Boolean}
*/
export function validChineseChar (str) {
  const pattern = /^[\u0391-\uFFE5]+$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}

/**
 * 对公账号 宽松验证
 * @param {string} str
 * @returns {Boolean}
 */
 export function validCorporateAccount (str) {
  const pattern = /^[0-9]\d{9,29}$/
  console.log(pattern.test(str), str)
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}

/**
 * 经度验证
 * @param {string} str
 * @returns {Boolean}
 */
export function validLongitude(str) {
  const pattern = /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,15})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,15}|180)$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}



/**
 * 纬度验证
 * @param {string} str
 * @returns {Boolean}
 */
export function validLatitude(str) {
  const pattern = /^(\-|\+)?([0-8]?\d{1}\.\d{0,15}|90\.0{0,15}|[0-8]?\d{1}|90)$/
  if (str !== '' && !pattern.test(str)) {
    return false
  } else {
    return true
  }
}
